package com.woniu.userassembly.controller;



import com.woniu.ly.model.common.Result;
import com.woniu.ly.model.model.BusinessLicense;
import com.woniu.ly.model.model.Card;
import com.woniu.userassembly.identifycontroller.impl.BusinessLicenses;
import com.woniu.userassembly.identifycontroller.impl.HouseholdRegister;
import com.woniu.userassembly.identifycontroller.impl.IdCard;
import com.woniu.userassembly.identifycontroller.impl.Upload;
import com.woniu.userassembly.service.TBusinessLicenseService;
import com.woniu.userassembly.service.TCardService;
import com.woniu.userassembly.service.UserService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

//识别上传的证件图片信息
@RestController
public class identifyController {
   @Autowired
   private UserService userService;
   @Autowired
   private TCardService tCardService;
    @Resource
    private TBusinessLicenseService tBusinessLicenseService;

    //识别身份证正面
    public static Card identifyFront(MultipartFile file) throws JSONException {
        Upload upload = new Upload();
        String filePath = upload.uploadCard(file);
        String idcard = IdCard.idcard(filePath,"front");

        JSONObject jsonObject = new JSONObject(idcard);
        JSONObject card = jsonObject.getJSONObject("words_result");
        String cardHolder = card.getJSONObject("姓名").getString("words");
        String cardNumber = card.getJSONObject("公民身份号码").getString("words");
        Card card1 = new Card();
        card1.setCardNum(cardNumber);
        card1.setCardHolder(cardHolder);
        card1.setCardType("身份证");

//      userService.addCard(card1);

//        int cardId = card1.getCardId();
//        System.out.println(cardId);
        return card1;
    }
    //识别身份证背面
    public static Result identifyBack(@RequestParam("card") MultipartFile file) throws JSONException, ParseException {
        Upload upload = new Upload();
        String filePath = upload.uploadCard(file);
        String idcard = IdCard.idcard(filePath,"back");

        JSONObject jsonObject = new JSONObject(idcard);
        JSONObject card = jsonObject.getJSONObject("words_result");
        String time = card.getJSONObject("失效日期").getString("words");
        String time2 = time + " 00:00:00";


        SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = oldFormat.parse(time2);
        String format = newFormat.format(date);

        Timestamp ts = Timestamp.valueOf(format);



        return Result.success(ts);
    }

    //识别营业执照
    public Result identifyBusinessLicense(@RequestParam("businesslicense") MultipartFile file) throws JSONException, ParseException {
        Upload upload = new Upload();
        String filePath = upload.uploadCard(file);
        String businessLicense = BusinessLicenses.businessLicense(filePath);

        JSONObject jsonObject = new JSONObject(businessLicense);
        JSONObject license = jsonObject.getJSONObject("words_result");
        String businessLicenseName = license.getJSONObject("单位名称").getString("words");
        String businessLicenseAddress = license.getJSONObject("地址").getString("words");
        String businessLicensePeople = license.getJSONObject("法人").getString("words");
        String businessLicenseMoney = license.getJSONObject("注册资本").getString("words");
        String businessLicenseField = license.getJSONObject("经营范围").getString("words");
        String businessLicenseTime = license.getJSONObject("成立日期").getString("words");

        String time = businessLicenseTime + " 00:00:00";

        SimpleDateFormat oldFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = oldFormat.parse(time);
        String format = newFormat.format(date);

        Timestamp ts = Timestamp.valueOf(format);

        BusinessLicense businessLicense1 = new BusinessLicense();
        businessLicense1.setBusinessLicenseAddress(businessLicenseAddress);
        businessLicense1.setBusinessLicenseName(businessLicenseName);
        businessLicense1.setBusinessLicensePeople(businessLicensePeople);
        businessLicense1.setBusinessLicenseMoney(businessLicenseMoney);
        businessLicense1.setBusinessLicenseField(businessLicenseField);
        businessLicense1.setBusinessLlicenseTime(ts);

        boolean b = tBusinessLicenseService.save(businessLicense1);

        return Result.success(b);
    }

    //识别户口本
    public Result identifyHouseholdRegister(@RequestParam("household") MultipartFile file) throws JSONException {
        Upload upload = new Upload();
        String filePath = upload.uploadCard(file);
        String householdRegister = HouseholdRegister.householdRegister(filePath);

        JSONObject jsonObject = new JSONObject(householdRegister);
        JSONObject household = jsonObject.getJSONObject("words_result");
        String cardHolder = household.getJSONObject("Name").getString("words");
        String cardNumber = household.getJSONObject("CardNo").getString("words");
        Card card1 = new Card();
        card1.setCardNum(cardNumber);
        card1.setCardHolder(cardHolder);
        card1.setCardType("户口本");

        tCardService.save(card1);
        int i = card1.getCardId();

        return Result.success(card1);
    }


}
